package app.auto.runner.base.utility.base;

import android.os.SystemClock;
import android.view.View;

import app.auto.runner.base.intf.FunCallback;

public class DoubleClickListener implements View.OnClickListener {

    private final FunCallback single;
    FunCallback fun;

    public DoubleClickListener(FunCallback fun, FunCallback single) {
        this.fun = fun;
        setFun(fun);
        this.single = single;
    }

    public void setFun(FunCallback fun) {
        this.fun = fun;
    }

    long[] mHits = new long[2];

    public void onClick(View view) {
        //实现数组的位移操作，点击一次，左移一位，末尾补上当前开机时间（cpu时间）
        System.arraycopy(mHits, 1, mHits, 0, mHits.length - 1);
        mHits[mHits.length - 1] = SystemClock.uptimeMillis();

        if (350 > (SystemClock.uptimeMillis() - mHits[0])) {
            //此处执行双击事件
            if (fun != null) {
                fun.simpleRun(view);
            }
        } else {
            if (single != null) {
                single.simpleRun(view);
            }
        }
    }
}
